<?php
namespace Home\Controller;

use Think\Controller;

class UserController extends Controller
{

    public function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
// 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];
        }
    }

    public function zhuce()
    {
        if (IS_POST) {
            $info         = $this->upload('pic');
            $_POST['pic'] = $info;
            if (!$this->check_verify(I('post.yzm'))) {
                $this->error('验证码错误');
            }
            $form = D('User');

            $data = $form->create();
            if ($data) {
                if ($id=$form->add($data)) {
                  
                        $this->email($id,$data['email']);
                    $this->success('注册成功', U('Home/User/denglu'));

                } else {
                    $this->error('注册失败');
                }

            } else {
                $this->error($form->getError());
            }
        } else {
            $this->display();
        }
    }
    public function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function yzm()
    {
        $config = array(
            'fontSize' => 30, // 验证码字体大小

            'length'   => 2, // 验证码位数
            'useNoise' => true, // 关闭验证码杂点
        );
        $yzm = new \Think\Verify($config);
        $yzm->entry();
    }

    public function denglu()
    {
        if (IS_GET) {
            $this->display();
        } else {
            $form = D('User');
            $res  = $form->where(['zh' => I('post.zh'), 'pwd' => md5(I('post.pwd'))])->find();
            if ($res) {
                session('islogin', 1);
                unset($res['pwd']);
                session('res', $res);

                $this->success('欢迎回来', U('Home/Index/index'));
            } else {
                $this->error('登陆失败', U('Home/User/denglu'));
            }

        }
    }

    public function email($id, $email)
    {
        import('Org.Net.Mail');

        $url = 'http://tp.me/' . U('Home/User/active', ['id' => $id]);
        $info = '<a href=' . $url . '>中奖人员名单确认</a>';

        if (SendMail($email, '中奖人员名单确认', $info, '福利彩票中心')) {

        } else {
            $this->error('发送失败');
        }
    }

    public function active()
    {
        $id = I('get.id');
        $form = D('User');
        $res  = $form->where(['id' => $id])->setField('status', 1);
        if ($res) {
            $this->success('激活成功');
        } else {
            $this->error('激活失败');
        }
    }

}
